import { _decorator, Button, Node } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { Prefab } from 'cc';
import { instantiate } from 'cc';
import { StageXButton } from './StageXButton';
import { ScrollView } from 'cc';
import { optimizeScrollView } from '../../../../scripts/libs/utils/ScrollView/OptimizeScrollView';
import { StringTools } from '../../../../scripts/libs/utils/StringTools';
import { lm } from '../../LobbyManager';
import { ResourceHelper } from '../../ResourceHelper';
import { FlowStatus } from '../../Enums';
import { gm } from '../../../../scripts/managers/GameManager';
import { DevConfig } from '../../../../scripts/managers/DevConfig';

const { ccclass, property } = _decorator;

@ccclass('EditorStageSelectPanel')
export class EditorStageSelectPanel extends BasePanel {

    @property(Prefab)
    stageXButtonPrefab: Prefab = null;

    @property(Node)
    container: Node = null;

    @property(ScrollView)
    scrollView: ScrollView = null;

    willBind(): void {
        optimizeScrollView(this.scrollView);
    }

    // static _alreadyAutoStart = false;

    onLoadView(): void {
        let stageIds = Array.from({ length: DevConfig.STAGE_NUM_MAX + 1 }, (_, _idx) => _idx);
        for (let i = 0; i < stageIds.length; i++) {
            let stageId = stageIds[i];
            let stageXButtonNode = instantiate(this.stageXButtonPrefab);
            stageXButtonNode.getComponent(StageXButton).stageId.value = stageId;
            this.container.addChild(stageXButtonNode);
        }
        // !LobbyPanel._alreadyAutoStart && this.checkAutoStartBySearch();
    }

    // checkAutoStartBySearch() {
    //     LobbyPanel._alreadyAutoStart = true;
    //     let stageStr = StringTools.getValueInSearch('stage');
    //     if (stageStr != null) {
    //         let stageId = Number(stageStr);
    //         lm.customLoadingReasons.add('load_stage');
    //         ResourceHelper.LoadStageAssets(stageId).subscribe(stageInfo => {
    //             if (stageInfo != null) {
    //                 lm.customLoadingReasons.delete('load_stage');
    //                 gm.stageInfo = stageInfo;
    //                 lm.flowStatus.value = FlowStatus.Room;
    //             } else {
    //                 lm.customLoadingReasons.delete('load_stage');
    //             }
    //         });
    //     }
    // }

}

